#include "mythread.h"
#include <QDebug>

MyThread::MyThread() : running(true)
{
    _client = new Client("109.12.116.178", 50002, 50003);

    if(_client->isConnectedMain && _client->isConnectedGame)
        message = "--- Connecte au serveur ---";
    else
        message = "--- Deconnecte du serveur ---";
}

MyThread::~MyThread()
{
    delete _client;
}

void MyThread::run()
{
    while (running)
    {
        if(_client->_selector.wait(sf::milliseconds(60)))
        {
            sf::Packet p;
            if(_client->_selector.isReady(_client->_sockMainServ))
            {
                _client->recv(p, true);
                traitementMain(p);
            }
            if(_client->_selector.isReady(_client->_sockGameServ))
            {
                _client->recv(p, false);
                traitementGame(p);
            }
        }
    }
}

void MyThread::traitementMain(sf::Packet & p)
{
    int x;
    p >> x;

    if (DEBUG) {std::cout << "Packet recu (main) --- evenement " << x << std::endl;}

    switch(x)
    {
        case eMAIN_NEWCLIENT :
            receptionClient();
            break;

        case eMAIN_NEWGAME :
            receptionPartie();
            break;

        case eMAIN_NEWMSG :
            receptionMessage();
            break;

        case eMAIN_DECOCLIENT :
            decoClient();
            break;

        case eMAIN_DECOGAME :
            decoPartie();
            break;

        case eMAIN_ALLCLIENTS :
        {
            sf::Packet p2;
            _client->recv(p2, true);
            int nbClients;
            p2 >> nbClients;
            for (int i(0); i < nbClients; i++)
            {
                receptionClient();
            }
        } break;

        case eMAIN_ALLGAMES :
        {
            sf::Packet p2;
            _client->recv(p2, true);
            int nbGames;
            p2 >> nbGames;
            for (int i(0); i < nbGames; i++)
            {
                receptionPartie();
            }
        } break;

        case eLAUNCH :
        {
            receptionCarte();
        } break;

        case eCREATE_GAME :
        {
            traitementNllePartie();
        } break;

        case eMAIN_ADDPARTIE :
        {
            traitementAddPartie();
        } break;

        case eMAIN_RMPARTIE :
        {
            traitementRmPartie();
        } break;

        case eMAIN_NEWCLSALON :
        {

        } break;

        case eMAIN_DELCLSALON :
        {

        } break;

        default :
            if (DEBUG) {std::cout << "case default du switch main du thread" << std::endl;}
            break;
    }
}

void MyThread::traitementGame(sf::Packet & p)
{
    sf::Uint8 x;
    p >> x;

    if (DEBUG) {std::cout << "Packet recu (game) --- evenement " << x << std::endl;}

    switch(x)
    {
        default :
            if (DEBUG) {std::cout << "case default du switch game du thread" << std::endl;}
            break;
    }
}

void MyThread::receptionClient()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> nomClient;
    emit newClient();
}

void MyThread::receptionPartie()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> nomPartie >> nbJAct >> nbJMax;
    emit newGame();
}

void MyThread::receptionMessage()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> message;
    emit newMsg();
}

void MyThread::decoClient()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> nomClient;
    emit decoC();
}

void MyThread::decoPartie()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> nomPartie;
    emit decoP();
}

void MyThread::receptionCarte()
{
    // ... recevoir les nb aleatoires de la map ...
    emit launch();
}

void MyThread::traitementNllePartie()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> valeurPartie;
    emit createGame();
}

void MyThread::traitementAddPartie()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> nomPartie;
    emit addPartie();
}

void MyThread::traitementRmPartie()
{
    sf::Packet p2;
    _client->recv(p2, true);
    p2 >> nomPartie;
    emit rmPartie();
}

void MyThread::quitter()
{
    sf::Packet p;
    p << eMAIN_DECOCLIENT;
    _client->send(p, true);

    running = false;
}

std::string MyThread::getNomClient()
{
    return nomClient;
}

std::string MyThread::getNomPartie()
{
    return nomPartie;
}

std::string MyThread::getMessage()
{
    return message;
}

sf::Uint8 MyThread::getNbJAct()
{
    return nbJAct;
}

sf::Uint8 MyThread::getNbJMax()
{
    return nbJMax;
}

Client* MyThread::getClient()
{
    return _client;
}

sf::Uint8 MyThread::getValeurPartie()
{
    return valeurPartie;
}
